﻿using HCApp.UI.Windows.Views;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Linq;
using System.Collections.Generic;
using Syncfusion.Windows.Tools.Controls;
using System;

namespace MainAppEntryPrismWpf.Views
{
    /// <summary>
    /// Interaction logic for YGSignInPage
    /// </summary>
    public partial class YGSignInPage : ContentPageBase
    {

        public DataClassesSCDBDataContext SCDBDataContent;

        private int MaxBH = -1;

        private DSZX_YGXXB SelectedYG = new DSZX_YGXXB();

        private List<DSZX_YGXXB> ygxxList;    //所有列表  同数据库中数据同
        private List<DSZX_YGXXB> ygSignedList;
        public YGSignInPage()
        {
            InitializeComponent();
            
            ygSignedList = new List<DSZX_YGXXB>();
            ygxxList = new List<DSZX_YGXXB>();

            SCDBDataContent = new DataClassesSCDBDataContext();         

            datagrid.ItemsSource = (from data in SCDBDataContent.DSZX_YGXXB
                                     where data.CJH== "单丝"
                                    select data).ToList();
        }

        private void add_Click(object sender, RoutedEventArgs e)
        {
            DateTime certainDate = Convert.ToDateTime(sfDatePicker.Value?.ToString("yyyy-MM-dd"));
            DateTime certainDateNext = certainDate.AddDays(1);

            //   var fdfCJH = (this.cjSelect.SelectedItem as ComboBoxItemAdv).Content.ToString();

            var datePick = this.sfDatePicker.Value;

            DateTime a = (DateTime)datePick;

       

            var currentYG = new DSZX_YGXXB()
            {
            
            };

           // SCDBDataContent.DSZX_YGXXB.InsertOnSubmit(currentYG);
          //  SCDBDataContent.SubmitChanges();

            //ygxxList = (from data
            //           in SCDBDataContent.DSZX_YGXXB
            //            select data).ToList();
            //datagrid.ItemsSource = ygxxList;

            var stringTotal = string.Empty;

            if (ygSignedList.Count > 0)
            {
                foreach (var item in ygSignedList)
                {
                    DSZX_CLGZB ToAddRecord = new DSZX_CLGZB();
                    ToAddRecord.CJH = item.CJH;
                    ToAddRecord.SCRQ = a;
                    ToAddRecord.YGXM = item.YGXM;
                    ToAddRecord.YGBH = item.YGBH;
                    
                    ToAddRecord.BC = (this.ggBC.SelectedItem as ComboBoxItem).Content.ToString();

                    //var xx = SCDBDataContent.DSZX_CLGZB.FirstOrDefault(x => x.YGBH == ToAddRecord.YGBH && x.SCRQ >= certainDate && x.SCRQ < certainDateNext);
                    //if (xx!=null)
                    //{
                    //    continue;
                    //}
                    SCDBDataContent.DSZX_CLGZB.InsertOnSubmit(ToAddRecord);
                   SCDBDataContent.SubmitChanges();

                    stringTotal += item.YGXM + " ,";
                }
            }
            if (stringTotal!="")
            {
                totalNameSigned.Text = stringTotal + "签到成功";
            }

           

        }
        private void datagrid_SelectionChanged(object sender, Syncfusion.UI.Xaml.Grid.GridSelectionChangedEventArgs e)
        {

            ygSignedList.Clear();
            for (int i = 0; i < this.datagrid.SelectedItems.Count; i++)
            {
                ygSignedList.Add(this.datagrid.SelectedItems[i] as DSZX_YGXXB);
            }
            
            //更新控件信息，便于修改用户信息

        }

        private void cjSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if (this.cjSelect.SelectedIndex != -1)
            //{
            //    ygxxList = SCDBDataContent.DSZX_YGXXB.Where(a => a.CJH == (this.cjSelect.SelectedItem as ComboBoxItem).Content.ToString() && a.CJH == "制线").ToList();
            //    ygxxList = (from data in SCDBDataContent.DSZX_YGXXB
            //                select data).ToList();
            //    datagrid.ItemsSource = ygxxList;
            //}

        }

        private void ContentPageBase_Loaded(object sender, RoutedEventArgs e)
        {
            datagrid.ItemsSource = (from data in SCDBDataContent.DSZX_YGXXB
                                    where data.CJH == "单丝"
                                    select data).ToList();
        }
    }
}
